package com.qdsg.ysg.doctor.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.ui.GroupManageActivity;
import com.qdsg.ysg.doctor.ui.PatientDetailActivity;
import com.qdsg.ysg.doctor.ui.SearchPatientActivity;
import com.rest.business.RestProxy;
import com.rest.response.GroupResponse;
import com.rest.response.Patient;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import drawthink.expandablerecyclerview.adapter.BaseRecyclerViewAdapter;
import drawthink.expandablerecyclerview.bean.RecyclerViewData;
import drawthink.expandablerecyclerview.holder.BaseViewHolder;
import drawthink.expandablerecyclerview.listener.OnRecyclerViewListener;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseFragment;

public class ContactsFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerview;
    @BindView(R.id.edt_search)
    TextView edt_search;

    private List<RecyclerViewData> mDatas = new ArrayList<>();
    private List<GroupResponse.Group> groupList = new ArrayList<>();
    private PatientAdapter adapter;
    private int currentGroup;


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_contacts;
    }


    public static void Logout() {
        contactsFragment = null;
    }

    @Override
    protected void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager( getActivity() );
        linearLayoutManager.setOrientation( LinearLayoutManager.VERTICAL );
        mRecyclerview.setLayoutManager( linearLayoutManager );
//        mRecyclerview.addItemDecoration(new SimplePaddingDecoration(getActivity()));

        adapter = new PatientAdapter( getActivity(), mDatas );
        adapter.setOnItemClickListener( new OnRecyclerViewListener.OnItemClickListener() {
            @Override
            public void onGroupItemClick(int position, int groupPosition, View view) {
                if (groupPosition == currentGroup) {
                    currentGroup = -1;
                } else {
                    currentGroup = groupPosition;
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildItemClick(int position, int groupPosition, int childPosition, View view) {
                Bundle bundle = new Bundle();
                bundle.putString( "id", ((Patient) mDatas.get( groupPosition ).getChild( childPosition )).id );
                startActivity( PatientDetailActivity.class, bundle );
            }
        } );
        adapter.setOnItemLongClickListener( null );
        mRecyclerview.setAdapter( adapter );
        adapter.notifyDataSetChanged();
//        edt_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//                    mDatas.clear();
//                    for (int i = 0; i < groupList.size(); i++) {
//                        List<Patient> tempList = new ArrayList<>();
//                        for (int j = 0; j < groupList.get(i).groupData.size(); j++) {
//                            if (groupList.get(i).groupData.get(j).patientName.indexOf(edt_search.getText().toString()) != -1) {
//                                tempList.add(groupList.get(i).groupData.get(j));
//                            }
//                        }
//                        mDatas.add(new RecyclerViewData(groupList.get(i).groupName+","+tempList.size(), tempList, false));
//                    }
//
//                    adapter.notifyRecyclerViewData();
//                    adapter.notifyDataSetChanged();
//                    SoftKeyboardUtil.hideSoftKeyboard(activity);
//                }
//                return false;
//            }
//        });
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        getGroupList();
        super.onResume();
    }

    @OnClick(R.id.btn_add)
    void btn_add() {
        startActivity( GroupManageActivity.class );
    }

    @OnClick(R.id.edt_search)
    void edt_search() {
        startActivity( SearchPatientActivity.class );
    }

    class PatientAdapter extends BaseRecyclerViewAdapter<String, Patient, PatientViewHolder> {

        private LayoutInflater mInflater;

        PatientAdapter(Context ctx, List<RecyclerViewData> datas) {
            super( ctx, datas );
            mInflater = LayoutInflater.from( ctx );
        }

        /**
         * head View数据设置
         *
         */
        @SuppressLint("SetTextI18n")
        @Override
        public void onBindGroupHolder(PatientViewHolder holder, int groupPos, int position, String groupData) {
            if (groupPos == currentGroup) {
                holder.tvTitle.setTextColor( getResources().getColor( R.color.mainGreen ) );
                holder.xiala.setImageResource( R.mipmap.xiala2 );
            } else {
                holder.tvTitle.setTextColor( getResources().getColor( R.color.commonBlack ) );
                holder.xiala.setImageResource( R.mipmap.xiala );
            }
            holder.tvTitle.setText( groupData.split( "," )[0] );
            holder.tv_num.setText( groupData.split( "," )[1] + "人" );

        }

        /**
         * child View数据设置
         *
         */
        @SuppressLint("SetTextI18n")
        @Override
        public void onBindChildpHolder(PatientViewHolder holder, int groupPos, int childPos, int position, Patient childData) {
            holder.tvName.setText( childData.patientName );
            holder.tv_detail.setText( childData.sex + " " + childData.age );
            holder.tv_message.setText( "最近主诊断：" + childData.diagnoseName );
            holder.tv_time.setText( childData.updateTime );
        }

        @Override
        public View getGroupView(ViewGroup parent) {
            return mInflater.inflate( R.layout.title_item_layout, parent, false );
        }

        @Override
        public View getChildView(ViewGroup parent) {
            return mInflater.inflate( R.layout.item_layout, parent, false );
        }

        @Override
        public PatientViewHolder createRealViewHolder(Context ctx, View view, int viewType) {
            return new PatientViewHolder( ctx, view, viewType );
        }

        /**
         * true 全部可展开
         * fasle  同一时间只能展开一个
         */
        @Override
        public boolean canExpandAll() {
            return false;
        }
    }


    static class PatientViewHolder extends BaseViewHolder {

        TextView tvName,tvTitle, tv_detail, tv_message, tv_time, tv_num;
        ImageView img_head, xiala;


        PatientViewHolder(Context ctx, View itemView, int viewType) {
            super( ctx, itemView, viewType );
            tvName = (TextView) itemView.findViewById( R.id.tv_name );
            tvTitle = (TextView) itemView.findViewById( R.id.tv_title );
            tv_detail = itemView.findViewById( R.id.tv_detail );
            tv_message = itemView.findViewById( R.id.tv_message );
            tv_time = itemView.findViewById( R.id.tv_time );
            tv_num = itemView.findViewById( R.id.tv_num );
            xiala = itemView.findViewById( R.id.xiala );
        }

        @Override
        public int getGroupViewResId() {
            return R.id.group;
        }

        @Override
        public int getChildViewResId() {
            return R.id.child;
        }
    }


    private void getGroupList() {
        RestProxy.getInstance().getGroupList( new Observer<GroupResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GroupResponse groupResponse) {
                mDatas.clear();
                groupList.clear();
                currentGroup = -1;
                groupList.addAll( groupResponse.data );
                for (int i = 0; i < groupResponse.data.size(); i++) {
                    mDatas.add( new RecyclerViewData( groupList.get( i ).groupName + "," + groupList.get( i ).num, groupResponse.data.get( i ).groupData, false ) );
                }
                adapter.notifyRecyclerViewData();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        } );
    }

    @SuppressLint("StaticFieldLeak")
    private static ContactsFragment contactsFragment;

    public static ContactsFragment newInstance() {
        if (contactsFragment == null) {
            contactsFragment = new ContactsFragment();
            return contactsFragment;
        } else {
            return contactsFragment;
        }
    }
}
